package com.example.blog.controller.blog;

import com.example.blog.pojo.Blog;
import com.example.blog.pojo.Result;
import com.example.blog.service.BlogService;
import com.example.blog.util.PcUtils.UploadFileUtils;
import com.example.blog.util.TokenUtil.GetCookie;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;

;

@RestController
@RequestMapping("/Blogs")
@Slf4j
public class BlogController {
    @Resource
    private BlogService blogService;

    //博客发布接口
    @PostMapping("/add")
    public Result<T> saveComment(Blog blog, @RequestParam String label, HttpServletRequest request) throws ServletException, IOException {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        Part filePart = request.getPart("avatar");
        if (filePart != null) {
            UploadFileUtils.BlogResult result = UploadFileUtils.savePart(request, filePart);
            blog.setCoverurl(result.getUrl());
        }
        return blogService.saveBlog(blog, label, token);
    }

    //展示全部博客接口
    @GetMapping("/get")
    public Result getComment(@RequestParam int pageNum, @RequestParam int pageSize) {
        return blogService.showBlog(pageNum, pageSize);
    }


    @GetMapping("/getAllBlogs")
    public Result<List<Blog>> getAllBlog(){
        return blogService.getAllBlog();
    }

    //根据id查询博客详情
    @GetMapping("/getById/{id}")
    public Result<Blog> getById(@PathVariable int id) {
        return blogService.getById(id);
    }

    //获取当前登录用户历史博客
    @GetMapping("/getUser")
    public Result getUserComment(HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return blogService.getUserBlog(token);
    }

    //根据博客作者查询发布过的博客
    @GetMapping("/getByUserName")
    public Result getByUserName(@RequestParam String author) {
        return blogService.getBlogsByAuthor(author);
    }

    //根据id删除博客
    @DeleteMapping("/delete/{id}")
    public Result deleteCommentRecord(@PathVariable int id, HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return blogService.deleteBlogRecord(id, token);
    }

    //查询博客根据关键词
    @GetMapping("/getByKeyword")
    public Result getByKeyword(@RequestParam String keyword,@RequestParam int pageNum,@RequestParam int pageSize) {
        return blogService.getByKeyword(keyword,pageNum,pageSize);
    }

    //根据标签查询博客
    @GetMapping("/getBlogByLabel")
    public Result getLabel(@RequestParam String label,@RequestParam int pageNum, @RequestParam int pageSize) {
        return blogService.getBlogByLabel(label,pageNum,pageSize);
    }

    //点赞接口
    @PostMapping("/likes/{id}")
    public Result likeComments(@PathVariable int id, HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return blogService.likeBlogs(id, token);
    }

    //收藏接口
    @PostMapping("/collectBlog/{blogId}")
    public Result<T> CollectBlog(@PathVariable int blogId, HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return blogService.CollectBlog(blogId, token);
    }

}